package com.leo.javase.thread.itcast;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 线程范围内共享数据
 * @author Lian
 */
public class ThreadScopeShareData {
	private static int data = 0;
	private static Map<Thread,Integer> map = new HashMap<Thread,Integer>();
	/**
	 * main方法
	 * @param args
	 */
	public static void main(String[] args) {
		for(int i=0; i<2; i++){
			new Thread(new Runnable() {
				@Override
				public void run() {
					int data = new Random().nextInt(10000);
					System.out.println(Thread.currentThread().getName()+" has put data:"+data);
					map.put(Thread.currentThread(), data);
					new A().get();
					new B().get();
				}
			}).start();
		}
	}
	/**
	 * 模块A
	 * @author Lian
	 */
	static class A{
		public void get(){
			int data = map.get(Thread.currentThread());
			System.out.println("A from "+Thread.currentThread().getName()+" has get data:"+data);
		}
	}
	/**
	 * 模块B
	 * @author Lian
	 */
	static class B{
		public void get(){
			int data = map.get(Thread.currentThread());
			System.out.println("B from "+Thread.currentThread().getName()+" has get data:"+data);
		}
	}
}
